﻿using System;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using cz.zcu.PreSti.DirectX;
using System.Windows.Forms;
using System.Drawing;
using cz.zcu.PreSti.XNADirectX;

namespace cz.zcu.PreSti.Components
{
    public class InputMouseComponent : RunnableComponent
    {
        #region Fields
        private bool enabled = false;
        private XNADirectXPresentation xnaPresentation = null;
        private bool leftButtonEnabled = false;
        private bool rightButtonEnabled = false;
        private Point position;
        private MouseEventHandler mouseClickedHandler;
        private MouseEventHandler mouseMovedHandler;

        #endregion

        #region properties

        public bool LeftButtonEnabled
        {
            get
            {
                return leftButtonEnabled;
            }
            set
            {
                leftButtonEnabled = value;
            }
        }

        public bool RightButtonEnabled
        {
            get
            {
                return rightButtonEnabled;
            }
            set
            {
                rightButtonEnabled = value;
            }
        }

        #endregion

        public InputMouseComponent(IPresentation p)
            : base(p, 2, 1, 0, 2)
        {
            if (p is XNADirectXPresentation) xnaPresentation = (XNADirectXPresentation)p;
            Priority = 10;
            Finished += new EventHandler(onFinished);
            mouseClickedHandler = new MouseEventHandler(mouseClicked);
            mouseMovedHandler = new MouseEventHandler(mouseClicked);
        }

        public override void update() { }

        public override void trigIn(int n)
        {
            if (xnaPresentation != null)
            {
                if (n == 0 && !enabled)
                {
                    xnaPresentation.WindowCont.MouseClick += mouseClicked;
                    xnaPresentation.WindowCont.MouseMove += mouseMoved;
                    enabled = true;
                }

                if (n == 1 && enabled)
                {
                    xnaPresentation.WindowCont.MouseClick -= mouseClicked;
                    xnaPresentation.WindowCont.MouseMove -= mouseMoved;
                    enabled = true;
                }
            }
        }

        private void onFinished(object sender, EventArgs e) { }

        private void mouseClicked(object sender, MouseEventArgs e)
        {
            if (leftButtonEnabled && e.Button == MouseButtons.Left || RightButtonEnabled && e.Button == MouseButtons.Right)
                trigOut(0);
        }

        private void mouseMoved(object sender, MouseEventArgs e)
        {
            position = e.Location;
        }

        public override object getValueFromGetter(int getterN)
        {
            switch (getterN)
            {
                case 0:
                    return position.X;
                case 1:
                    return position.Y;
                default:
                    return base.getValueFromGetter(getterN);
            }
        }
    }
}
